package com.swz.dcrm.util;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class PermissionUtil {

    private static String mPermissions[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA};
    public static boolean  needCarmaAndSdCard(Fragment fragment){
        List<String> permissionsList = new ArrayList<>();
            if(Build.VERSION.SDK_INT <Build.VERSION_CODES.M){
                return false;
            }
        for (int i = 0; i < mPermissions.length; i++) {
            //判断一个权限是否已经允许授权，如果没有授权就会将单个未授权的权限添加到List里面
            if (ContextCompat.checkSelfPermission(fragment.getContext(), mPermissions[i]) != PackageManager.PERMISSION_GRANTED) {
                permissionsList.add(mPermissions[i]);
            }

            if (!permissionsList.isEmpty()) {
                String [] permissions = permissionsList.toArray(new String[permissionsList.size()]);
                //执行授权的代码。此处执行后会弹窗授权
                fragment.requestPermissions( permissions, Constant.REQ_WRITE_EXTERNAL_STORAGE_CAMERA);
                return true;
            }
        }
        return false;
    }
}
